// app.js
App({
    onLaunch() {
        // 展示本地存储能力
        var self=this;   
        this.globalData.updateStatus=1;
        const capsule = wx.getMenuButtonBoundingClientRect(); 
        console.log(capsule);  
        self.globalData.capsule=capsule; 
        
       let res= wx.getWindowInfo();
      console.log(res);
            if (res.screenHeight - res.windowHeight - res.statusBarHeight - 32 > 72) {
              self.globalData.isFullSucreen = true
            }
            let navHeight = res.statusBarHeight
            if(res.platform == 'ios' || res.platform=='devtools'){
              self.globalData.platform = 'ios';            
            }else if(res.platform == 'android'){
              self.globalData.platform = 'android';
            }
            self.globalData.pixelRatio = res.pixelRatio;
            self.globalData.navHeight = capsule.height;
            self.globalData.navTitleTop = capsule.top;
            self.globalData.navWidth = capsule.width;
            self.globalData.navRight =capsule.right;
            self.globalData.bottom = capsule.bottom;
            self.globalData.statusBarHeight=res.statusBarHeight;
            self.globalData.windowHeight=res.windowHeight;
            self.globalData.screenHeight=res.screenHeight;
            self.globalData.windowWidth=res.windowWidth-2;
            self.globalData.screenWidth=res.screenWidth-2;

        
          
   
    
    
      },
      autoUpdate:function(){
        var that = this;
        return new Promise(function(resolve, reject) {
          let startParamObj = wx.getLaunchOptionsSync(); 
          console.log('startParamObj=>'+JSON.stringify(startParamObj));
          if(wx.canIUse('getUpdateManager') && startParamObj.scene!=1154){//获取小程序更新机制兼容 
           const updateManager=wx.getUpdateManager();
          //判断改小程序是否有新版本
          updateManager.onCheckForUpdate(function(res){
              console.log(res);
            //如果有新版本，更新
            if(res.hasUpdate){
              //准备更新
              updateManager.onUpdateReady(function(){
                wx.showModal({
                  title: '更新提示',
                  content: '检测到新版本，是否下载新版本并重启小程序',
                success:function(res){
                  if(res.confirm){
                     that.downLoadAndUpdate()         
                  }else if(res.cancel){
                      wx.showModal({
                          title: '提示',
                          content: '本次版本更新涉及到新功能的添加，旧版本将无法正常使用',
                          showCancel: false, // 隐藏取消按钮
                          confirmText: '确认更新', // 只保留更新按钮
                          success: function (res) {
                            if (res.confirm) {
                              // 下载新版本，重启应用
                              that.downLoadAndUpdate(updateManager)
                            }
                          }
                        })
             
                  }
                }
                })
              })
          
            }else{
             //console.log('=====>no update');
             resolve('=====>no update');
            }
          })
          }
        })
      },
        // 下载小程序最新版本并重启
    downLoadAndUpdate: function (updateManager) {
      wx.showLoading()
      // 静默下载更新小程序新版本，onUpdateReady：当新版本下载完成回调
      updateManager.onUpdateReady(function () {
        wx.hideLoading()
        // applyUpdate：强制当前小程序应用上新版本并重启
        updateManager.applyUpdate()
      })
      // onUpdateFailed：当新版本下载失败回调
      updateManager.onUpdateFailed(function () {
        // 下载新版本失败
        wx.showModal({
          title: '已有新版本',
          content: '新版本已经上线了，请删除当前小程序，重新搜索打开',
        })
      })
    }, 
    request(url,data,method){
        let that = this;
      return new Promise(function (resove,reject){
          wx.request({
            url: that.globalData.platUrl+url,
            data:data,
            method:method,
            success:res=>{
                if(res.statusCode==200){
                  resove(res.data)
                } 
            },
            error:e=>{
                reject(e)
            }
          })
      })
    },
    goto:function(url='#',index='#'){
      console.log(url);
      console.log(index)
      if(url!="#"){
  
            if(index=='index'){
                wx.reLaunch({
                    url: url,
                })
            }else{
                wx.navigateTo({
                    url: url,
                    success:res=>{
                        console.log(res);
                    },
                    fail:res=>{
                        console.log(res)
                    }
                })
            }
      }
     
      }, 
   showModal:function(msg,url,index){
       let that = this;
      wx.showModal({
       // cancelColor: 'cancelColor',
        title:'系统提示',
        content:msg,
        //showCancel:false,
        success:function(e){
          if (e.confirm) {
            that.goto(url,index);
          }
        }
        
  
      })
  
  },

    globalData: {
        open: null,
        domain:'https://zq.browin.net.cn/',
        img:'https://zqimg.browin.net.cn',
        appUrl:'https://zq.browin.net.cn/wechat/',
      }
})
